package com.py.androidUtils.UI.adapter;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.py.androidUtils.UI.model.UserInfoView;
import com.py.androidUtils.base.BaseCommAdapter;
import com.py.androidUtils.utils.LoadingImages;
import com.py.androidUtils.utils.ToastUtil;
import com.readyidu.app.R;

public class GroupUserAdapter extends BaseCommAdapter<UserInfoView> {
	private LayoutInflater inflater;
	private Context mContext;
	public boolean isDelete = false;

	public GroupUserAdapter(Context context) {
		inflater = LayoutInflater.from(context);
		this.mContext = context;
	}

	public void setDelete() {
		if (isDelete) {
			isDelete = false;
			UserInfoView infoView = new UserInfoView();
			infoView.setType(1);
			listData.add(infoView);
			UserInfoView infoView2 = new UserInfoView();
			infoView2.setType(2);
			listData.add(infoView2);
			notifyDataSetChanged();
		} else {
			isDelete = true;
			List<UserInfoView> list = new ArrayList<UserInfoView>();
			for (int i = 0; i < listData.size(); i++) {
				if (((UserInfoView) listData.get(i)).getType() == 0) {
					list.add(((UserInfoView) listData.get(i)));
				}
			}
			clear();
			setList(list);
			notifyDataSetChanged();
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.common_group_user, null);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.headPortrait = (ImageView) convertView.findViewById(R.id.headPortrait);
			holder.deleteItem = (ImageView) convertView.findViewById(R.id.deleteItem);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		UserInfoView infoView = (UserInfoView) listData.get(position);
		if (infoView != null) {
			if (infoView.getType() == 0) {
				holder.name.setText(infoView.getName());
				LoadingImages.getInstance().loadingImages(infoView.getImg(), holder.headPortrait, mContext.getResources().getDimensionPixelSize(R.dimen.size_56));
			} else if (infoView.getType() == 1) {
				holder.headPortrait.setImageResource(R.drawable.group_add);
				holder.name.setText("");
			} else if (infoView.getType() == 2) {
				holder.headPortrait.setImageResource(R.drawable.group_subtract);
				holder.name.setText("");
			}
		}
		if (isDelete) {
			if (position == 0) {
				holder.deleteItem.setVisibility(View.GONE);
			} else {
				holder.deleteItem.setVisibility(View.VISIBLE);
			}
		} else {
			holder.deleteItem.setVisibility(View.GONE);
		}
		holder.deleteItem.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ToastUtil.showToast(mContext, "删除成员");
			}
		});
		return convertView;
	}

	private final class ViewHolder {
		private TextView name;
		private ImageView headPortrait;
		private ImageView deleteItem;
	}
}
